package com.floreysoft.jmte.staticCompiler.sample;

import java.math.BigDecimal;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import com.floreysoft.jmte.NamedRenderer;
import com.floreysoft.jmte.Renderer;
import com.floreysoft.jmte.RendererRegistry;
import com.floreysoft.jmte.realLife.model.Customer;
import com.floreysoft.jmte.realLife.model.Item;
import com.floreysoft.jmte.realLife.model.Order;

public class EmailStringProvider {

	private final Renderer<String> stringRenderer;
	private final Renderer<Date> dateRenderer;
	private final Renderer<Integer> integerRenderer;
	private final NamedRenderer currencyRenderer;
	
	public EmailStringProvider(RendererRegistry registry) {
		stringRenderer = registry.resolveRendererForClass(String.class);
		dateRenderer = registry.resolveRendererForClass(Date.class);
		integerRenderer = registry.resolveRendererForClass(Integer.class);
		currencyRenderer = registry.resolveNamedRenderer("currency");
	}

	/*
	${@define order: com.floreysoft.jmte.realLive.model.Order}
	${@define separator: java.lang.String}

	${-- This is a comment
	}${order.customer.firstName(dear customer)}!

	On ${order.orderDate} we received the following order:

	Shipping address:
	${, order.customer.firstName, }${order.customer.lastName}
	${order.customer.address}

	${foreach order.items item
	}${item.amount}x ${item.article.name} à ${item.article.price;currency} = ${item.subTotal;currency}${
	}${end}
	${if !order.shippingCost=0}plus shipping = ${order.shippingCost;currency}${end}
	${separator}
	Total = ${order.total;currency}

	Thank you for ordering!
		 
		 */
	public String toString(final Order order, final String separator) {
		final StringBuilder buffer = new StringBuilder();
		
		buffer.append("Hello, ");
		Customer customer = order.getCustomer();
		String firstName = customer.firstName;
		if (firstName == null || firstName.equals("")) {
			buffer.append("dear customer");
		} else {
			buffer.append(stringRenderer.render(firstName));
		}
		buffer.append("!\n\n");
		buffer.append("On ");
		buffer.append(dateRenderer.render(order.getOrderDate()));
		buffer.append(" we received the following order:\n\n");
		buffer.append("Shipping address:\n");
		if (firstName != null && !firstName.equals("")) {
			buffer.append(stringRenderer.render(firstName));
			buffer.append(" ");
		}
		buffer.append(stringRenderer.render(customer.lastName));
		buffer.append("\n");
		buffer.append(stringRenderer.render(customer.address));
		buffer.append("\n\n");

//		${foreach order.items item
//		}${item.amount}x ${item.article.name} à ${item.article.price;currency} = ${item.subTotal;currency}${
//		}${end}
		List<Item> items = order.getItems();
		for (Iterator<Item> iterator = items.iterator(); iterator.hasNext();) {
			Item item = iterator.next();
			buffer.append(integerRenderer.render(item.amount));
			buffer.append("x ");
			buffer.append(stringRenderer.render(item.article.name));
			buffer.append(" ");
			buffer.append(currencyRenderer.render(item.article.price, null));
			buffer.append(" each = ");
			buffer.append(currencyRenderer.render(item.getSubTotal(), null));
			
			if (iterator.hasNext()) {
				buffer.append("\n");
			}
		}
		buffer.append("\n");

//		${if !order.shippingCost=0}plus shipping = ${order.shippingCost;currency}${end}
//		${separator}
//		Total = ${order.total;currency}
		BigDecimal shippingCost = order.getShippingCost();
		if (shippingCost != null && shippingCost.toString().equals("0")) {
			buffer.append("plus shipping = ");
			buffer.append(currencyRenderer.render(shippingCost, null));
		}
		buffer.append("\n");
		buffer.append(separator);

//		Total = ${order.total;currency}
		
		buffer.append("\n");
		buffer.append("Total = ");
		buffer.append(currencyRenderer.render(order.getTotal(), null));

		
		buffer.append("\n\nThank you for ordering!");
		return buffer.toString();
	}
}
